<!DOCTYPE html>
<!--解决idea thymeleaf 表达式模板报红波浪线-->
<!--suppress ALL -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- 引入公用部分 -->
    <script th:replace="common/head::static"></script>
</head>
<body>

<form class="layui-form" id="addForm">

    <div class="layui-form-item">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-block">
            <input type="text" name="scenicId" th:value="${scenic.getScenicId()}" required lay-verify="required"
                   placeholder="景点编号" autocomplete="off" readonly
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">景点图片</label>
        <div class="layui-input-block">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="test1">上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo1">
                    <p id="demoText"></p>
                </div>
            </div>
            <input type="hidden" name="scenicPic" th:value="${scenic.getScenicPic()}" id="pic">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">景点名称</label>
        <div class="layui-input-block">
            <input type="text" name="scenicName" required lay-verify="required" placeholder="酒店名称"
                   th:value="${scenic.getScenicName()}" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">景点简介</label>
        <div class="layui-input-block">
            <textarea name="scenicDesc" placeholder="酒店简介" th:text="${scenic.getScenicDesc()}"
                      class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">修改</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>

<script>
    //Demo
    layui.use(['form', 'upload'], function () {
        var $ = layui.jquery
            , upload = layui.upload;
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function (data) {
            $.ajax({
                url: ctx + '/travel/scenic/save',
                method: 'post',
                data: $("#addForm").serialize(),
                success: (res) => {
                    layer.msg("修改成功")
                    setTimeout(function(){  // 设置定时器
                        self.location.href = ctx + "/travel/scenic/display";
                    }, 2000);
                }
            })
            return false;
        });


        var uploadInst = upload.render({
            elem: '#test1'
            , url: ctx + '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                console.log(res)
                $("#pic").val(res.data.accessPath)

            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

    });


</script>

</body>
<!-- js -->

</html>